#include "DevilTexture.h"
#include <iostream>
using namespace std;

DevilTexture::DevilTexture()
{
    if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION)
    {
        /* wrong DevIL version */
        cout << "Wrong devil version\n";
    }
    ilInit();

    this->isLoaded = false;
}

DevilTexture::~DevilTexture()
{
    
}

const wchar_t *Char2wchar(char *s) {
    int n = 1;
    while(*s++) {
        ++n;
    }
    s -= n;
    wchar_t *r = new wchar_t[n];
    for(int i=0; i<n; ++i) {
        r[i] = s[i];
    }
    return r;
}

int DevilTexture::Load(const char *fileName)
{
    ilGenImages(1, &ilTexID); /* Generation of one texID name */
    ilBindImage(ilTexID); /* Binding of texID name */
    success = ilLoadImage(fileName); /* Loading of texID "texID.jpg" */
    if (success) /* If no error occured: */
    {
        success = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE); /* Convert every colour component into
            unsigned byte. If your texID contains alpha channel you can replace IL_RGB with IL_RGBA */
        if(!success)
        {
            /* Error occured */
            cout << "Could not convert image\n";
            return 1;
        }
        glGenTextures(1, &texID); /* Texture name generation */
        glBindTexture(GL_TEXTURE_2D, texID); /* Binding of texture name */
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); /* We will use linear
            interpolation for magnification filter */
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); /* We will use linear
            interpolation for minifying filter */
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH),
            ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE,
            ilGetData()); /* Texture specification */

        width = ilGetInteger(IL_IMAGE_WIDTH);
        height = ilGetInteger(IL_IMAGE_HEIGHT); 
    }
    else
    {
        /* Error occured */
        cout << "OpenGL texture generation failed\n";
        return 1;
    }
    ilDeleteImages(1, &ilTexID); /* Because we have already copied texID data into texture data
        we can release memory used by texID. */
    this->isLoaded = true;
    return 0;
}

void DevilTexture::Use()
{
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texID);
}

bool DevilTexture::IsLoaded()
{
    return this->isLoaded;
}

int DevilTexture::GetTextureID()
{
    if(this->isLoaded)
        return this->texID;
    else
        return -1;
}

int DevilTexture::GetWidth()
{
    if(this->isLoaded)
        return this->width;
    else
        return -1;
}

int DevilTexture::GetHeight()
{
    if(this->isLoaded)
        return this->height;
    else
        return -1;	
}
